package com.e3.basdoc.goodscls.client;

import java.util.ArrayList;
import java.util.List;

import com.e3.basdoc.goodscls.model.GoodsClsModel;
import com.e3.template.tgf.TGFHelper;
import com.e3.web.widget.client.MyFormFieldFactory;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.RowNumberer;

public class GoodsClsHelper extends TGFHelper<GoodsClsModel> {
	
	public static GoodsClsHelper INSTANCE = new GoodsClsHelper();
	
	private String[] notNullFields = {GoodsClsModel.CODE,GoodsClsModel.NAME};

	public Field<?> createFormField(String name) {
		Field<?> formField = null;
		String dispName = getFieldDisplayName(name);
		boolean isNotNull = isNotNullField(name);
		if (name.equals(GoodsClsModel.ID)) {
			formField = MyFormFieldFactory.createIdField(name);
		} else if (name.equals(GoodsClsModel.CODE)) {
			formField = MyFormFieldFactory.createStringField(name, dispName,
					20, isNotNull);
		} else if (name.equals(GoodsClsModel.NAME)) {
			formField = MyFormFieldFactory.createStringField(name, dispName,
					50, isNotNull);
		} else if (name.equals(GoodsClsModel.AVERAGE_COST)
				|| name.equals(GoodsClsModel.AVERAGE_LEADTIME)
				|| name.equals(GoodsClsModel.AVERAGE_PRICE)) {
			formField = MyFormFieldFactory.createDoubleField(name, dispName, 2,
					19, isNotNull);
		} else if (name.equals(GoodsClsModel.PARENT_GOODSCLS)) {
			formField = MyFormFieldFactory.createRefField(name, dispName,
					new GoodsClsRefDialog(), isNotNull);
		} else if (name.equals(GoodsClsModel.REMARK)) {
			formField = MyFormFieldFactory.createTextAreaField(name, dispName,
					100, isNotNull);
		}

		return formField;
	}

	public List<ColumnConfig> getGridColumnConfigs() {
		List<ColumnConfig> configs = new ArrayList<ColumnConfig>();
		RowNumberer numberer = new RowNumberer();
		configs.add(numberer);

		ColumnConfig column = new ColumnConfig();
		String name = GoodsClsModel.CODE;
		column.setId(name);
		column.setHeader(getFieldDisplayName(name));
		column.setWidth(200);
		configs.add(column);

		column = new ColumnConfig();
		name = GoodsClsModel.NAME;
		column.setId(name);
		column.setHeader(getFieldDisplayName(name));
		column.setWidth(200);
		configs.add(column);

		column = new ColumnConfig();
		name = GoodsClsModel.AVERAGE_PRICE;
		column.setId(name);
		column.setHeader(getFieldDisplayName(name));
		column.setWidth(200);
		configs.add(column);

		column = new ColumnConfig();
		name = GoodsClsModel.AVERAGE_COST;
		column.setId(name);
		column.setHeader(getFieldDisplayName(name));
		column.setWidth(200);
		configs.add(column);

		column = new ColumnConfig();
		name = GoodsClsModel.AVERAGE_LEADTIME;
		column.setId(name);
		column.setHeader(getFieldDisplayName(name));
		column.setWidth(200);
		configs.add(column);

		column = new ColumnConfig();
		name = GoodsClsModel.PARENT_GOODSCLS;
		column.setId(name);
		column.setHeader(getFieldDisplayName(name));
		column.setWidth(200);
		configs.add(column);

		column = new ColumnConfig();
		name = GoodsClsModel.REMARK;
		column.setId(name);
		column.setHeader(getFieldDisplayName(name));
		column.setWidth(200);
		configs.add(column);

		return configs;
	}

	public String getFieldDisplayName(String code) {
		return GoodsClsFieldsConstant.INSTANCE.getString(code);
	}

	public void setModelRelations(GoodsClsModel pModel,
			List<GoodsClsModel> children) {
		// pModule.setSubModules(children);
		for (GoodsClsModel model : children) {
			model.setParentModel(pModel);
		}
	}

	@Override
	public String[] getNotNullFieldNames() {
		return notNullFields;
	}

	@Override
	public GoodsClsModel createModelInstance() {
		return new GoodsClsModel();
	}

	@Override
	public String getCodeFieldName() {
		return GoodsClsModel.CODE;
	}
}
